import { createRouter, createWebHashHistory, routerKey } from 'vue-router'
import Home from '@/views/Home/Home.vue'
import useStore from '@/store'
import Message from '@/components/XtxMessage'
const router = createRouter({
  scrollBehavior: () => {
    return { top: 0 }
  },
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: () => import('@/views/Layout/Layout.vue'),
      children:[
        {
          path: '', // 默认显示的内容
          component: Home
        },
        {
          // 动态路由
          // category/123  ====> 
          // category/abcdd  ====> 
          path: 'category/:id', 
          component: () => import('@/views/Category/Category.vue')
        },
        {
          // 详情页
          path: 'product/:id', 
          component: () => import('@/views/Goods/Goods.vue')
        },
        {
          // 购物车
          path: 'cart',
          component: () => import('@/views/Cart/Cart.vue')
        },
        {
          // 结算
          path: 'member/checkout',
          component: () => import('@/views/Member/Pay/Checkout.vue')
        },
        {
          // 支付
          path: 'member/pay',
          component: () => import('@/views/Member/Pay/Pay.vue')
        }
      ]
    },
    {
      path: '/login',
      component: () => import('@/views/Login/Login.vue')
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/Callback.vue')
    },
    {
      path: '/play',
      component: () => import('@/views/Playground.vue')
    },
    {
      path: '/test',
      component: () => import('@/Test/test.vue')
    }
  ]
})


router.beforeEach((to, from, next) =>{
  const { cart } = useStore()
  // to去哪个页面？
  console.log(to.path)
  // 没有登录的用户就不能访问/member/checkout
  if(cart.isLogin === false && to.path === '/member/checkout'){
    Message.warning('没有登录，不能访问')
    const url = '/login?redirect=' + to.fullPath
    next(url)
  }else {
    next()
  }
})
export default router
